// index.js
// 获取应用实例
const app = getApp()
var mqtt = require('../../utils/mqtt.min.js')
var client = null

Page({
  data: {
  },
  onLoad() {
    this.connectMqtt()
  },
  connectMqtt:function()
  {
    const options = {
      connectTimeout:4000,
      clientId:'my_client_id',
      port:8084,
      username:'a49077641f69609cba98968a8f55f0af',
      password:'123'
    }
    client = mqtt.connect('wxs://t.yoyolife.fun/mqtt', options)
    client.on('connect', (e)=>{
      console.log('服务器连接成功')
      client.subscribe('/iot/97/remote_control/pub', {
        qos:0
      },function(err){
        if(!err)
        {
          console.log('订阅成功')
        }
      })
    })
    client.on('message',function(topic,message){
      console.log('收到'+message.toString())
    })

    client.on('reconnect',(error) => {
      console.log('正在重连接', error)
    })

    client.on('error',(error) => {
      console.log('连接失败', error)
    })
  },
  taphere()
  {
    console.log('hello world')
  }
})
